﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using System.Xml.Serialization;

//using System.Runtime.Serialization.Formatters.Soap;

namespace booktest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public enum PurchaseOrderStates
        {
            Issued,
            Delivered,
            Invoiced,
            Paid
        }

        [Serializable]
        public class Company
        {
            // ReSharper disable InconsistentNaming
            public string name;
            public string address;
            public string phone;
            // ReSharper restore InconsistentNaming
        }

        [Serializable]
        public class LineItem
        {
            // ReSharper disable InconsistentNaming
            public string description;
            public int quantity;
            public double cost;
            // ReSharper restore InconsistentNaming
        }

        [Serializable]
        public class PurchaseOrder
        {
            private PurchaseOrderStates _purchaseOrderStatus;
            private DateTime _issuanceDate;
            private DateTime _deliveryDate;
            private DateTime _invoiceDate;
            private DateTime _paymentDate;
            // ReSharper disable InconsistentNaming
            public Company buyer;
            public Company vendor;
            public string reference;
            public LineItem[] items;
            // ReSharper restore InconsistentNaming
            public PurchaseOrder()
            {
                _purchaseOrderStatus = PurchaseOrderStates.Issued;
                _issuanceDate = DateTime.Now;
            }

            public void RecordDelivery()
            {
                if (_purchaseOrderStatus == PurchaseOrderStates.Issued)
                {
                    _purchaseOrderStatus = PurchaseOrderStates.Delivered;
                    _deliveryDate = DateTime.Now;
                }
            }

            public void RecordInvoice()
            {
                if (_purchaseOrderStatus == PurchaseOrderStates.Delivered)
                {
                    _purchaseOrderStatus = PurchaseOrderStates.Invoiced;
                    _invoiceDate = DateTime.Now;
                }
            }
            public void RecordPayment()
            {
                if (_purchaseOrderStatus == PurchaseOrderStates.Invoiced)
                {
                    _purchaseOrderStatus = PurchaseOrderStates.Paid;
                    _paymentDate = DateTime.Now;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Company vendor = new Company();
            Company buyer = new Company();
            LineItem goods = new LineItem();
            PurchaseOrder po = new PurchaseOrder();
            vendor.name = "Acme Inc.";
            buyer.name = "Wiley E. Coyote";
            goods.description = "anti-RoadRunner cannon";
            goods.quantity = 1;
            goods.cost = 599.99;
            po.items = new LineItem[1];
            po.items[0] = goods;
            po.buyer = buyer;
            po.vendor = vendor;

            XmlSerializer xs = new XmlSerializer(po.GetType());
            FileStream fs = File.Create(@"C:\Users\HP\Pictures\po1.xml");
            xs.Serialize(fs, po);
            fs.Close();

            //BinaryFormatter bf = new BinaryFormatter();
            //FileStream fs = File.Create(@"C:\Users\HP\Pictures\po.bin");
            //bf.Serialize(fs, po);
            //fs.Close();

            //SoapFormatter sf = new SoapFormatter();
            //FileStream fs = File.Create(@"C:\Users\HP\Pictures\po.xml");
            //sf.Serialize(fs, po);
            //fs.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PurchaseOrder po = new PurchaseOrder();
            XmlSerializer xs = new XmlSerializer(po.GetType());
            FileStream fs = File.OpenRead(@"C:\Users\HP\Pictures\po1.xml");
            po = (PurchaseOrder)xs.Deserialize(fs);
            fs.Close();
            MessageBox.Show(@"Customer is " + po.buyer.name);

            //BinaryFormatter bf = new BinaryFormatter();
            //FileStream fs = File.OpenRead(@"C:\Users\HP\Pictures\po.bin");
            //PurchaseOrder po = (PurchaseOrder)bf.Deserialize(fs);
            //fs.Close();

            //SoapFormatter sf = new SoapFormatter();
            //FileStream fs = File.OpenRead(@"C:\Users\HP\Pictures\po.xml");
            //PurchaseOrder po = (PurchaseOrder)sf.Deserialize(fs);
            //fs.Close();

            MessageBox.Show(@"Customer is " + po.buyer.name);
        }
    }
}
